package dk.lector.domain;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import dk.lector.R;
import dk.lector.datasource.ConfigDBRepository;
import dk.lector.remote.TSMobileService;
import dk.lector.utils.TSProgressDialog;

public class ConfigFacade {
	
	public static void fetchConfig(final Context context, final String url) {

		AsyncTask<Void, String, Void> ConfigurationDownloaderTask = new AsyncTask<Void, String, Void>() {

			@Override
			protected void onPreExecute() {
				TSProgressDialog.showDialog(context,
						R.string.config_downloading_text);
				super.onPreExecute();
			}

			@Override
			protected Void doInBackground(Void... params) {
				
					TSMobileService.fetchConfig(context, url);
					//TODO: Clean user data.
					ConfigDBRepository.saveConfig(context);
					ConfigDBRepository.getValue(context, "ssl");

				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				TSProgressDialog.dismissDialog();
				context.sendBroadcast(new Intent(context
						.getString(R.string.ACTION_CONFIG_DOWNLOADED)));
				super.onPostExecute(result);
			}

		};
		ConfigurationDownloaderTask.execute();

	}

}
